package com.campuscrowdsourcing.server.controller;

import cn.hutool.core.util.DesensitizedUtil;
import com.campuscrowdsourcing.server.annotation.LoginCheck;
import com.campuscrowdsourcing.server.common.constant.CommonConst;
import com.campuscrowdsourcing.server.common.constant.RoleConst;
import com.campuscrowdsourcing.server.common.enums.Action;
import com.campuscrowdsourcing.server.domain.dto.user.LoginDTO;
import com.campuscrowdsourcing.server.domain.model.AjaxResult;
import com.campuscrowdsourcing.server.domain.vo.TokenVO;
import com.campuscrowdsourcing.server.domain.vo.UserVO;
import com.campuscrowdsourcing.server.service.UserService;
import lombok.extern.java.Log;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 *
 * @author XinYuan Zhao
 * @since 2023/3/9
 */
@RestController
@RequestMapping("/api/user")
public class UserController {


    private final UserService userService;

    public UserController(UserService userService) {
        this.userService = userService;
    }


    /**
     * 登录权限校验测试
     * @return
     */
    @LoginCheck(RoleConst.ROLE_ADMIN)
    @GetMapping("/test")
    public AjaxResult test(){
        return AjaxResult.success("你好");
    }

    /**
     * 密码登录
     *
     * @param loginDto 登录信息
     * @return 用户信息
     */
    @PostMapping("/login")
    public AjaxResult<UserVO> login(@RequestBody @Validated LoginDTO loginDto) {
        if(!StringUtils.hasText(loginDto.getPassword())){
            return AjaxResult.fail();
        }
        return AjaxResult.success(userService.login(loginDto));
    }

    /**
     * 用户注册
     *
     * @param loginDTO 注册参数
     * @return 用户信息
     */
    @PostMapping("/register")
    public AjaxResult<UserVO> register(@RequestBody @Validated LoginDTO loginDTO) {
        return AjaxResult.success(userService.register(loginDTO));
    }


    /**
     * 获取验证码
     *
     * @param account 邮箱/手机号
     */
    @PostMapping("/code")
    public AjaxResult<UserVO> getCode(@RequestParam String account) {
        //TODO 根据IP存入Redis进行限制 同一IP一天最多获取10次, 10分钟内最多获取3次
        if(!StringUtils.hasText(account)){
            AjaxResult.fail();
        }else{
            userService.getCode(account);
        }
        return AjaxResult.success();
    }

    /**
     * 修改密码
     *
     * @param loginDTO 密码和验证码
     */
    @LoginCheck
    @PostMapping("/password")
    public AjaxResult<UserVO> updatePassword(@RequestBody LoginDTO loginDTO) {
        if( loginDTO == null
                || !StringUtils.hasText(loginDTO.getPassword())
                || !StringUtils.hasText(loginDTO.getCode())){
            return AjaxResult.fail();
        }
        userService.updatePassword(loginDTO);
        return AjaxResult.success();
    }

    /**
     * 更新用户头像
     *
     * @param file 头像文件
     */
    @LoginCheck
    @PostMapping("/avatar/update")
    public AjaxResult updateAvatar(MultipartFile file){
        if(file == null){
            return AjaxResult.fail();
        }
        userService.updateAvatar(file);
        return AjaxResult.success();
    }

    /**
     * 刷新token
     * @return 双token
     */
//    @Log(title="刷新Token",action = Action.UPDATE)
    @GetMapping("/refresh")
    public AjaxResult<TokenVO> refreshToken(){
        return AjaxResult.success(userService.refreshToken());
    }

    @GetMapping("/teamUser/{id}")
    public AjaxResult listUserByTeamId(@PathVariable Integer id) {
        return AjaxResult.success(userService.getAllByTeamId(id));
    }

    @GetMapping("/teamRoot/{id}")
    public AjaxResult getRootUserByTeamId(@PathVariable Integer id) {
        return AjaxResult.success(userService.getByTeamId(id));
    }

}
